package com.yun.zf.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfHouseDocumentInfoMapper;
import com.yun.zf.admin.service.ZfHouseDocumentInfoService;
import com.yun.zf.common.admin.dto.house.DocumentCountDto;
import com.yun.zf.common.admin.model.house.DoucumentInfoMo;
import com.yun.zf.common.app.domain.ZfHouseDocumentInfo;
import com.yun.zf.common.constant.Constants;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.utils.AliyunOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 证件信息表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-01
 */
@Service
@Slf4j
public class ZfHouseDocumentInfoServiceImpl extends ServiceImpl<ZfHouseDocumentInfoMapper, ZfHouseDocumentInfo> implements ZfHouseDocumentInfoService {

    @Autowired
    private ZfHouseDocumentInfoMapper documentInfoMapper;
    @Autowired
    private AliyunOssUtil ossUtil;



    /**
     * 获取产证图片信息
     * @param basicId
     * @param provinceCode
     * @param cityCode
     * @return
     */
    @Override
    public List<DoucumentInfoMo> getDocumentInfo(Long basicId, Long provinceCode, Long cityCode ,int type) {

        return  documentInfoMapper.getDocuments(basicId,provinceCode,cityCode, type);
    }

    @Override
    public Response uploadDoucument(MultipartFile[] files, HttpServletRequest request, Long provinceCode, Long cityCode) {
        try {
            List<Map<String,String>> pictureList = new ArrayList<>();
            for (MultipartFile file : files
            ) {
                Map<String, String> map = ossUtil.uploadObject2OSS(file, Constants.BUCKET_CERTIFICATE, Constants.FOLDER_CERTIFICATE + "/" + request.getParameter("cid"));
                log.info("房源图片上传返回信息-----{}", JSONObject.toJSONString(map));
                if (map != null) {
                    ZfHouseDocumentInfo documentInfo = new ZfHouseDocumentInfo();
                    documentInfo.setBasicId(Long.parseLong(request.getParameter("cid")));
                    documentInfo.setCityCode(cityCode);
                    documentInfo.setProvinceCode(provinceCode);
                    documentInfo.setPath(map.get("path"));
                    documentInfo.setUrl(map.get("url"));
                    documentInfo.setType(Integer.parseInt(request.getParameter("type"))); // 设置类型为房产证
                    // 保存图片信息
                    baseMapper.insert(documentInfo);
                    Map<String,String> picMap = new HashMap<>();
                    picMap.put("id",String.valueOf(documentInfo.getId()));
                    picMap.put("url",map.get("url"));
                    pictureList.add(picMap);
                }

                return  Response.success(pictureList);
            }
        }catch (Exception e){

        }
        return null;
    }

    @Override
    public Response delDoucument(String ids, Long provinceCode, Long cityCode) {
        List<Long> idlist = Arrays.asList(ids.split(",")).parallelStream().map(a->Long.parseLong(a.trim())).collect(Collectors.toList());
        QueryWrapper<ZfHouseDocumentInfo>  wrapper = new QueryWrapper<>();
        wrapper.in("id",idlist).eq("city_code",cityCode).eq("province_code",provinceCode);
        List<ZfHouseDocumentInfo> dList = baseMapper.selectList(wrapper);
        //2、删除oss 图片
        for (ZfHouseDocumentInfo pic : dList
        ) {
            //1、删除oss 中的图片资源
            ossUtil.deleteFile(Constants.BUCKET_CERTIFICATE, pic.getPath());
        }
        int cn = baseMapper.delete(wrapper);
        if (cn > 0) {
            return Response.success("删除成功");
        }
        return Response.error("删除失败");
    }

    @Override
    public  List<DocumentCountDto> getDocumentInfo(Long basicId, Long provinceCode, Long cityCode) {
        return  baseMapper.getCountByType(basicId,provinceCode,cityCode);
    }
}
